package com.iflytek.uoamp.agent.monitor.find;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.log4j.Logger;

/**
 * 处理CMDExecutor提交的任务
 * @author ltzeng
 *
 */
public class CMDExecuteWorker {
	private final ExecutorService workerPool;
	private final AtomicLong threadCounter;
	private static final CMDExecuteWorker instance = new CMDExecuteWorker(2, 30);
	private CMDExecuteWorker(int threadCount, int taskTimeoutSeconds) {
		this.threadCounter = new AtomicLong(0);
		this.workerPool = Executors.newFixedThreadPool(threadCount, new ThreadFactory() {
			@Override
			public Thread newThread(Runnable r) {
				Thread t = new Thread(r);
				t.setName("CMDExecuteWorker thread pool - " + threadCounter.incrementAndGet());
				Logger.getRootLogger().info("Make a new CMDExecuteWorker thread : " + t.getName());
				return t;
			}
		});
	}
	
	/**
	 * 初始化方法，此处为空实现，在agent启动时执行以达到初始化线程池的目的
	 */
	public void init() {}
	
	public static CMDExecuteWorker getInstance() {
		return instance;
	}
	
	public void submit(Runnable task) {
		if (null == task) {
			return;
		}
		this.workerPool.submit(task);
	}
	
}
